home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / to_stream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-24  |  4.1 KB  |  146 lines

  1. /*
  2.  * Copyright (c) 1992    Jin Guojun
  3.  *
  4.  * Disclaimer:  No guarantees of performance accompany this software,
  5.  * nor is any responsibility assumed on the part of the authors.  All the
  6.  * software has been tested extensively and every effort has been made to
  7.  * insure its reliability.
  8.  *
  9.  * to_stream - convert an image to HIPS-JPEG format
  10.  *
  11.  * usage:    to_stream < idata > oseq
  12.  *
  13.  * to load:    cc -o to_stream to_stream.c -lscs5 -lccs -hipsh -lhips -ljpeg
  14.  *
  15.  * AUTHOR:    Jin Guojun - 11/12/92
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include "header.def"
  20. #include "imagedef.h"
  21.  
  22. static Flag_Format flagfmt[] =
  23. {
  24.     {"c", {LASTFLAG}, 0, {{PTBOOLEAN, "FALSE"}, LASTPARAMETER}},
  25. /*  {"f", {LASTFLAG}, 1, {{PTINT, "30", "frames / sec."}, LASTPARAMETER}}, */
  26.     {"n", {LASTFLAG}, 1, {{PTINT, "99999999", "number frames"}, LASTPARAMETER}},
  27.     {"j", {LASTFLAG}, 0, {{PTBOOLEAN, "FALSE"}, LASTPARAMETER}},
  28.     {"p", {LASTFLAG}, 0, {{PTINT, "0", "pad to 2**"}, LASTPARAMETER}},
  29.     {"Q", {LASTFLAG}, 1, {{PTINT, "75", "Q-factor"}, LASTPARAMETER}},
  30.     LASTFLAG};
  31.  
  32.  
  33. main(argc, argv)
  34. int    argc;
  35. char**    argv;
  36. {
  37. int    oform, qfact, pflag=-1;
  38. Boolean    jflag, ciflag, ccflag, coflag;
  39. int    frames, f, pad;
  40. sframe_header    fhd;
  41. Filename    filename;
  42. byte    *fakec;
  43. extern    U_IMAGE    uimg;
  44.  
  45.     Progname = strsave(*argv);
  46.     parseargs(argc, argv, flagfmt, &coflag, &frames, &jflag, &pflag, &qfact,
  47.     FFONE, &filename);
  48.  
  49.     jpeg_uimg_init(hfopenr(filename), HIPS, 0, coflag);
  50.     reset_readpipe(&uimg);
  51.     if ((*uimg.header_handle)(HEADER_READ, &uimg, 0, 0, Yes) < 0)
  52.     prgmerr('h', "Unknown image type");
  53.     if (frames > hhd.num_frame)
  54.     frames = hhd.num_frame / hhd.numcolor;
  55.     else    hhd.num_frame = frames * hhd.numcolor;
  56. /*
  57.     ciflag = hjpeg_color(&hhd);
  58. */
  59.     ciflag = isColorImage(uimg.in_color);
  60.     ccflag = coflag & !ciflag;
  61.  
  62.     if (get_sinfo(0, 0))    /* stream in    */
  63.     if ((pad=get_sinfo(1, 0)) == 1)    {    /* compress type for reading */
  64.         if (jflag)    prgmerr('j', "s_jpeg to s_jpeg - Why ?");
  65.         stream_jpeg_rinit(in_fp, get_sinfo_q(0), ciflag | coflag);
  66.         clearparam(&hhd, "JPEG-info");
  67.     } else if (pad=!jflag)
  68.         prgmerr(0, "Warning: stream to stream - waste time");
  69.  
  70.     set_stream_param(&hhd, get_stream_header_sline(hhd.ocols, hips_channels(&hhd)),
  71.         jflag, -1, qfact, -1 /* no data_size */, 1);
  72.  
  73.     if (!jflag && !pad)
  74.     if (coflag/* && hhd.numcolor == 1*/)    {
  75.         if (hhd.pixel_format != PFRGB)
  76.         hhd.sizeimage *= hips_channels(&hhd);
  77.         if (ciflag & pad)    {
  78.         hhd.pixel_format = PFRGB;
  79.         hhd.num_frame /= hhd.numcolor,    hhd.numcolor = 1;
  80.         }
  81.     } else    {
  82.         hhd.pixel_format = PFBYTE;
  83.         hhd.num_frame /= hhd.numcolor,    hhd.numcolor = 1;
  84.     }
  85.     else if (pad)    hhd.sizeimage *= hips_channels(&hhd);
  86.     write_headeru(&hhd, argc, argv);
  87. /*
  88.     (*uimg.header_handle)(HEADER_WRITE, &uimg, argc, argv, NULL);
  89. */
  90.     if (jflag)
  91.     hhjpeg_winit(stdout, &hhd, get_sinfo_q(1), ciflag | coflag);
  92.  
  93.     fakec = (byte *) nzalloc(uimg.width * uimg.height, 3);
  94.     if (ccflag)
  95.         hhd.image = (byte *) nzalloc(uimg.width * uimg.height, 3);
  96.     if (pflag < 0)    pflag = 0;
  97.     if (pflag)    {
  98.         for (pad=0, pflag=(pflag<<1)-1; pflag>>=1; pad++);
  99.         pflag = 1 << pad;
  100.     }
  101.  
  102.     for (f = 0; f < frames; f++) {
  103.     if (stream_jpeg_read_image(fakec, &hhd, f) < 1)
  104.         prgmerr('r', "stream_jpeg_read_image");
  105.     if (ccflag) {
  106.         register byte *sp = fakec, *cp = hhd.image;
  107.         register int r = uimg.height, w = uimg.width;
  108.         while (r--) {
  109.             memcpy(cp, sp, w);
  110.             cp += w;
  111.             memcpy(cp, sp, w);
  112.             cp += w;
  113.             memcpy(cp, sp, w);
  114.             cp += w;
  115.             sp += w;
  116.         }
  117.     } else    hhd.image = fakec;
  118.  
  119.     if (jflag) {
  120.         if (stream_jpeg_write_frame(hhd.image, &fhd) < 0)
  121.         return (perr(HE_WRITEFRFILE, f, filename));
  122.         /*
  123.          * this routine points hhd->image to the compressed image, and
  124.          * sets the size of the compressed frame in fhd
  125.          */
  126.         fhd.frame = f;
  127.         if (pflag)    {
  128.         pad = pflag - (fhd.size % pflag);
  129.         fprintf(stderr, "padding by %d bytes \n", pad);
  130.         fhd.size += pad;
  131.         }
  132.         if (write_stream_header(stdout, &hhd, &fhd, f) < 0
  133.             || fwrite(hhd.image, fhd.size, 1, stdout) != 1)
  134.         return (perr(HE_WRITEFRFILE, f, filename));
  135.  
  136.     } else    write_image(&hhd, f);
  137.     }
  138. if (jflag)
  139.     write_jpeg_eof(stdout);
  140. fclose(stdout);
  141. free(fakec);
  142. if (ccflag)
  143.     free(hhd.image);
  144. return (0);
  145. }
  146.